﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SharpShare.Activation {
    public sealed class ExtensionDescriptor<T> : IEquatable<ExtensionDescriptor<T>> {
        public ExtensionDescriptor(string name, Type type) {
            if (name == null) throw new ArgumentNullException("name");
            if (type == null) throw new ArgumentNullException("type");

            if (!typeof(T).IsAssignableFrom(type))
                throw new ArgumentException(string.Format("Type '{0}' cannot be assigned to type parameter of type '{1}'.", type, typeof(T)), "type");

            this.Name = name;
            this.ExtensionType = type;
        }

        public string Name { get; private set; }
        public string Description { get; set; }
        public Type ExtensionType { get; private set; }

        public T Create() {
            return (T)Activator.CreateInstance(this.ExtensionType);
        }

        #region IEquatable<ExtensionDescriptor<T>> Members

        public bool Equals(ExtensionDescriptor<T> other) {
            if (other == null) return false;
            return (this.Name.Equals(other.Name) && this.ExtensionType.Equals(other.ExtensionType));
        }

        #endregion

        public override bool Equals(object obj) {
            return this.Equals(obj as ExtensionDescriptor<T>);
        }

        public override int GetHashCode() {
            return (this.Name.GetHashCode() ^ this.ExtensionType.GetHashCode());
        }
    }
}
